package br.com.ads.sgse.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.com.ads.dao.DAO;
import br.com.ads.sgse.entity.Disciplina;



@ManagedBean
@ViewScoped
public class DisciplinaBean {
	private Disciplina disciplina = new  Disciplina();
	private List<Disciplina> disciplinas;
	private DAO<Disciplina> dao = new DAO<Disciplina>(Disciplina.class);
	
	/**  ACTIONS  **/
	
	@SuppressWarnings("unused")
	@PostConstruct
	private void init() {
		getAllDisciplinas();
	}
	
	
	public String salvar() {
	   try {
		   if (disciplina.getId() == null) {
			   dao.adiciona(disciplina);   
		   } else {
			   dao.atualiza(disciplina);
		   }
		   
		   getAllDisciplinas();
		   disciplina = new Disciplina();
	   } catch (Exception e){
		   e.printStackTrace();
	   }
	   return null;
	}
	
	public void remove() {
		try {
			dao.remove(disciplina);
			getAllDisciplinas();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getAllDisciplinas() {
		try {
			disciplinas = dao.listaTodos();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public Disciplina getDisciplina() {return disciplina;}
	public void setDisciplina(Disciplina disciplina) {this.disciplina = disciplina;}


	public List<Disciplina> getDisciplinas() {return disciplinas;}
	public void setDisciplinas(List<Disciplina> disciplinas) {this.disciplinas = disciplinas;}

}
